/*
 * Copyright (c) 2007
 *	Alberto Magni, Maggioni Marcello
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

package Database.XMLManager;

import Database.ConfigurationDB;
import exceptions.DatabaseException;
import java.io.File;

/**
 * This class is the implementation of the Configuration database using an XML representation of data.
 * 
 * @author Marcello Maggioni
 *
 */
public class XMLConfigurationFiles implements ConfigurationDB {

    private String filename = "configuration.xml";
    private String root_path;
    
    private XMLConfigurationFiles(String root_path) {
        this.root_path = root_path;
        File rp = new File(root_path);
        if(!rp.exists())
            rp.mkdirs();
    }
    
    /*public void changeUsername(String olduser, String newuser) {
        File oldfile = new File(root_path + File.separator + olduser);
        File newfile = new File(root_path + File.separator + newuser);
        if (oldfile.exists()) {
            newfile.mkdirs();
            File configfile = new File(root_path + File.separator + olduser + File.separator + "configuration.xml");
            File newconfigfile = new File(root_path + File.separator + olduser + File.separator + "configuration.xml");
            configfile.renameTo(newconfigfile);
            configfile.delete();
            oldfile.delete();
        }
    }*/
    
    public void setServer(String username, String server) throws DatabaseException {
        XMLConfigurationFile xmcf = getXMLConfigurationObjectWrite(username);
        xmcf.setServer(server);
    }

    public void setEMailAddress(String username, String email) throws DatabaseException {
        XMLConfigurationFile xmcf = getXMLConfigurationObjectWrite(username);
        
        xmcf.setEMailAddress(email);
    }
    
    public String getServer(String username) throws DatabaseException {
        XMLConfigurationFile xmcf = getXMLConfigurationObjectWrite(username);
        if (xmcf == null)
            return null;
        return xmcf.getServer();
        
    }

    public String getEMailAddress(String username) throws DatabaseException {
        XMLConfigurationFile xmcf = getXMLConfigurationObjectRead(username);
        if (xmcf == null)
            return null;
        return xmcf.getEMailAddress();
    }
    
    public void removeConfiguration(String username) {
        String path = null;
        if (username == null)
            path = root_path + File.separator + filename;
        else
            path = root_path + File.separator + username + File.separator + filename;
        
        File f = new File(path);
        if (f.exists())
            f.delete();
        f = f.getParentFile();
        if (f.exists())
            f.delete();
    }
    
     /**
     * Returns a default instance of an XMLConfigurationFiles object 
     *  
     * @param root_path The root path in which the files will be stored
     * @return An XMLConfigurationFiles instance
     */
    
    static public ConfigurationDB getDefaultInstance(String root_path) {
        return new XMLConfigurationFiles(root_path);
    }

    public void setSpamCheck(String username, boolean value) throws DatabaseException {
        XMLConfigurationFile xmcf = getXMLConfigurationObjectWrite(username);
        xmcf.setSpamCheck(value);
    }

    public boolean getSpamCheck(String username) throws DatabaseException {
        XMLConfigurationFile xmcf = getXMLConfigurationObjectRead(username);
        if (xmcf == null)
            return false;
        return xmcf.getSpamCheck();
        
    }

    public void setServerAuthMode(String username, boolean value) throws DatabaseException {
        XMLConfigurationFile xmcf = getXMLConfigurationObjectWrite(username);
        
        xmcf.setServerAuthMode(value);
    }

    public boolean getServerAuthMode(String username) throws DatabaseException {
        XMLConfigurationFile xmcf = getXMLConfigurationObjectRead(username);
        if (xmcf == null)
            return false;
        return xmcf.getServerAuthMode();
    }
    
        private XMLConfigurationFile getXMLConfigurationObjectWrite(String username) throws DatabaseException{
        try {
            String path = null;
            if (username == null) {
                path = root_path + File.separator + filename;
            } else {
                path = root_path + File.separator + username + File.separator + filename;
            }

            File f = new File(path);
            if (!f.getParentFile().exists()) {
                f.getParentFile().mkdirs();
            }

            return new XMLConfigurationFile(path);
        } catch (Exception ex) {
            throw new DatabaseException(ex.getMessage());
        }
    }
    
    private XMLConfigurationFile getXMLConfigurationObjectRead(String username) throws DatabaseException {
        try {
            String path = null;
            if (username == null) {
                path = root_path + File.separator + filename;
            } else {
                path = root_path + File.separator + username + File.separator + filename;
            }
            File f = new File(path);
            if (!f.exists())
                return null;
            
            return new XMLConfigurationFile(path);
        } catch (Exception ex) {
            throw new DatabaseException(ex.getMessage());
        }
    }
    
}
